'use strict';

const Controller = require('./baseController');

class articlesController extends Controller {
	//列表
	async list() {
		const ctx = this.ctx;
		var body = ctx.request.body
		console.log('body', body);
		let params = {
			offset:0,
			limit:10,
		}
		if(body.members_id){
			params.where = {members_id:body.members_id};
		}
		const articles = await ctx.service.articles.list(params);

		this.success(articles);
	}
	//保存
	async save() {
		const ctx = this.ctx;
		var body = ctx.request.body
		console.log('body', body);
		//验证登陆状态
		const user = await this.checkLogin(body.key);
		if (user) {
			body.members_id = user.id;
		} else {
			this.needLogin();
		}
		const article = await ctx.service.articles.save(body);

		console.log('res', article);
		this.success({});
	}
	//获取详情
	async info() {
		console.log('info');
		const ctx = this.ctx;
		var body = ctx.request.body
		let params = {
			id: body.id
		}
		const article = await ctx.service.articles.findOne(params);
		if (article) {
			this.success(user);
		} else {
			this.fail('笔记不存在');
		}
	}

	//更新
	async update() {
		console.log('info');
		const ctx = this.ctx;
		var body = ctx.request.body

		await ctx.service.articles.update(body);
		this.success({});

	}

	//删除
	async delete() {
		this.success({});
	}
}

module.exports = articlesController;